package com.hgyc.mom.eam.controller;

import java.time.LocalDateTime;
import java.util.*;

import cn.dev33.satoken.stp.StpUtil;
import com.hgyc.mom.common.enums.CommonOrderStatusEnum;
import com.hgyc.mom.eam.vo.EamCheckReportVO;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.eam.entity.EamMaintainReport;
import com.hgyc.mom.eam.vo.EamMaintainReportParam;
import com.hgyc.mom.eam.service.EamMaintainReportService;
import com.hgyc.mom.eam.vo.EamMaintainReportVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 设备保养记录Controller
 *
 * @author fwj
 * @since 2025-08-16
 */
@Tag(name = "设备保养记录控制器", description = "设备保养记录控制器")
@RestController
@RequestMapping("/eam/maintain/report")
public class EamMaintainReportController {
    @Autowired
    private EamMaintainReportService eamMaintainReportService;

    @Operation(summary = "查询设备保养记录列表", description = "查询设备保养记录列表")
    @PostMapping("/pageList")
    public Result<IPage<EamMaintainReport>> pageList(@Parameter(description = "查询参数") @RequestBody EamMaintainReportParam eamMaintainReportParam) {
        Page<EamMaintainReport> page = new Page<>(eamMaintainReportParam.getPage(), eamMaintainReportParam.getPageSize());
        EamMaintainReport eamMaintainReport = new EamMaintainReport();
        BeanUtils.copyProperties(eamMaintainReportParam, eamMaintainReport);
        QueryWrapper<EamMaintainReport> wrapper = QueryWrapperUtil.build(eamMaintainReport);
        IPage<EamMaintainReport> list = eamMaintainReportService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取设备保养记录详细信息", description = "获取设备保养记录详细信息")
    @GetMapping(value = "/{id}")
    public Result<EamMaintainReport> getInfo(@Parameter(description = "设备保养记录ID") @PathVariable("id") Long id) {
        return Result.success(eamMaintainReportService.getById(id));
    }
    @Operation(summary = "新增设备保养记录", description = "新增设备保养记录")
    @PostMapping
    public Result<?> save(@Parameter(description = "设备保养记录") @RequestBody EamMaintainReportVO eamMaintainReportVO) {
        eamMaintainReportService.saveReport(eamMaintainReportVO);
        return Result.success();
    }

    @Operation(summary = "修改设备保养记录", description = "修改设备保养记录")
    @PutMapping
    public Result<?> update(@Parameter(description = "设备保养记录") @RequestBody EamMaintainReportVO eamMaintainReportVO) {
        eamMaintainReportService.updateReport(eamMaintainReportVO);
        return Result.success();
    }

    @Operation(summary = "提交设备保养记录", description = "提交设备保养记录")
    @PostMapping("/submit")
    public Result<?> submit(@Parameter(description = "设备保养") @RequestBody EamMaintainReportVO eamMaintainReportVO) {
        eamMaintainReportVO.setStatus(CommonOrderStatusEnum.APPROVING.getValue());
        eamMaintainReportVO.setMaintainBy(StpUtil.getLoginIdAsLong());
        eamMaintainReportVO.setMaintainTime(LocalDateTime.now());
        eamMaintainReportService.updateReport(eamMaintainReportVO);
        return Result.success();
    }

    @Operation(summary = "更新状态", description = "更新状态")
    @PutMapping("/status/{status}")
    public Result<?> updateStatus(@PathVariable("status") String status, @RequestParam("ids") Long[] ids) {
        eamMaintainReportService.updateStatus(status, Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "删除设备保养记录", description = "删除设备保养记录")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        eamMaintainReportService.removeReport(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EamMaintainReport> util = new ExcelUtil<>(EamMaintainReport.class);
        util.importTemplateExcel(response, "设备保养记录");
    }

    @Operation(summary = "导出设备保养记录列表", description = "导出设备保养记录列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamMaintainReportParam eamMaintainReportParam) {
        EamMaintainReport eamMaintainReport = new EamMaintainReport();
        BeanUtils.copyProperties(eamMaintainReportParam, eamMaintainReport);
        QueryWrapper<EamMaintainReport> wrapper = QueryWrapperUtil.build(eamMaintainReport);
        List<EamMaintainReport> list = eamMaintainReportService.list(wrapper);
        ExcelUtil<EamMaintainReport> util = new ExcelUtil<>(EamMaintainReport.class);
        util.exportExcel(response, list, "设备保养记录数据");
    }

    @Operation(summary = "导入设备保养记录列表", description = "导入设备保养记录列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EamMaintainReport> util = new ExcelUtil<>(EamMaintainReport.class);
        List<EamMaintainReport> eamMaintainReportList = util.importExcel(file.getInputStream());
        String message = eamMaintainReportService.importData(eamMaintainReportList, updateSupport);
        return Result.success(message);
    }
}
